package com.qf.bbapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.bbapi.dao.TIntegralDao;
import com.qf.bbapi.dao.TTaskIntegralDao;
import com.qf.bbapi.dto.TaskDto;
import com.qf.bbapi.entity.TIntegral;
import com.qf.bbapi.entity.TTaskIntegral;
import com.qf.bbapi.service.intf.IntegralService;
import com.qf.bbapi.type.TaskType;
import com.qf.bbapi.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ````':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 *
 * @author 贾
 * @version 1.0
 * @description: TODO
 * @date 2022/7/2 19:53
 */
@Service
public class IntegralServiceImpl implements IntegralService {
    @Resource
    private TTaskIntegralDao dao;
    @Resource
    private TIntegralDao integralDao;

    @Override
    @Transactional
    public R signIn(TaskDto integral, int token) {
        if (integral != null && token > 0) {
            QueryWrapper<TIntegral> wrapper = new QueryWrapper<>();
            wrapper.eq("uid", token);
            TIntegral tIntegral = integralDao.selectOne(wrapper);
            if (tIntegral == null) {
                if (integralDao.insert(new TIntegral(token, integral.getIntegral())) > 0) {
                    if (dao.insert(new TTaskIntegral(token, TaskType.签到.getValue(), integral.getIntegral())) > 0) {
                        return R.ok("签到成功，积分+5");
                    }
                }
            }
            TTaskIntegral tTaskIntegral = dao.selectById(token, integral.getType());
            if (tTaskIntegral != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                if (!sdf.format(new Date()).equals(sdf.format(tTaskIntegral.getCtime()))) {
                    if (integralDao.updateByUid(tIntegral.getIntegral() + integral.getIntegral(), token) > 0) {
                        if (dao.insert(new TTaskIntegral(token, TaskType.签到.getValue(), integral.getIntegral())) > 0) {
                            return R.ok("签到成功，积分+5");
                        }
                    }
                }
            } else {
                if (integralDao.updateByUid(tIntegral.getIntegral() + integral.getIntegral(), token) > 0) {
                    if (dao.insert(new TTaskIntegral(token, TaskType.签到.getValue(), integral.getIntegral())) > 0) {
                        return R.ok("签到成功，积分+5");
                    }
                }
            }

        }
        return R.fail("签到失败");
    }

    @Override
    @Transactional
    public R shop(TaskDto integral, int token) {
        if (integral != null && token > 0) {
            QueryWrapper<TIntegral> wrapper = new QueryWrapper<>();
            wrapper.eq("uid", token);
            TIntegral tIntegral = integralDao.selectOne(wrapper);
            if (tIntegral == null) {
                if (integralDao.insert(new TIntegral(token, integral.getIntegral())) > 0) {
                    if (dao.insert(new TTaskIntegral(token, TaskType.购买商品.getValue(), integral.getIntegral())) > 0) {
                        return R.ok("积分+20");
                    }
                }
            } else {
                if (integralDao.updateByUid(tIntegral.getIntegral() + integral.getIntegral(), token) > 0) {
                    if (dao.insert(new TTaskIntegral(token, TaskType.购买商品.getValue(), integral.getIntegral())) > 0) {
                        return R.ok("积分+20");
                    }
                }
            }

        }
        return R.fail();
    }

    @Override
    public R select(int token) {
        if (token > 0) {
            TIntegral selectOne = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
            if (selectOne != null) {
                return R.ok(selectOne);
            } else {
                if (integralDao.insert(new TIntegral(token, 0)) > 0) {
                    TIntegral selectOne1 = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
                    if (selectOne1 != null) {
                        return R.ok(selectOne1);
                    }
                }

            }
        }
        return R.fail();
    }

    @Transactional
    @Override
    public R selectSingIn(TaskDto integral, int token) {
        if (integral != null && token > 0) {
            QueryWrapper<TIntegral> wrapper = new QueryWrapper<>();
            wrapper.eq("uid", token);
            TIntegral tIntegral = integralDao.selectOne(wrapper);
            if (tIntegral == null) {
                integralDao.insert(new TIntegral(token, 0));
                return R.ok();
            }
            TTaskIntegral tTaskIntegral = dao.selectById(token, integral.getType());
            if (tTaskIntegral == null) {
                return R.ok();
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                if (!sdf.format(new Date()).equals(sdf.format(tTaskIntegral.getCtime()))) {
                    return R.ok();
                }
            }

        }

        return R.fail();
    }
}